<?php
namespace Admin\Controller;
use  Think\Controller;

/**
* 
*/
class ChannelLoginController extends Controller
{
	
	public function loginz(){

        $account = I('post.user_name');
        $password = md5(I('post.password'));

//        $user = M('user');
        $user = M('channel');
        $user_info = $user->where(['user_name' => $account])->find();

        if($user_info === null){

            $ajaxReturn['code'] = 501;
            $ajaxReturn['msg'] = "账号不存在！";

        }else if($user_info['password'] != $password){

            $ajaxReturn['code'] = 502;
            $ajaxReturn['msg'] = "密码错误！";

        }else{

            $ajaxReturn['code'] = 200;
            $ajaxReturn['msg'] = "SUCCESS";
            session('user_info', $user_info);

        }
        $this->ajaxReturn($ajaxReturn);

    }

    public function ChannelCustomDataList(){
    	$start_time=I('post.start_time');
		$end_time=I('post.end_time');
    	$channel_id=I('post.id');//$user_info['id'];
    	$model=M('channel_data');

    	$starttime=array();
        $endtime=array();
        if(!empty($start_time)) {
            $starttime['add_time'] = array('egt',$start_time);
        }

         if(!empty($end_time)) {
            $endtime['add_time'] = array('lt',$end_time);
        }
		$page=I('post.page/d');
		$page_size=I('post.page_size/d');
		$count=$model->where('channel_id='.$channel_id)->count();
		$total=ceil($count/$page_size);
		$limitStart=($page-1)*$page_size;
		$settle_ment['settlement_status']="1";

    	$data=$model->field('add_time,ad_name,false_consumption_amount,false_num,cooperation_type')->where($settle_ment)->where('channel_id='.$channel_id)->limit($limitStart,$page_size)->select();
    	$data=array_map(function($val){
			if($val['cooperation_type']=='cps'){
				$val['data']=$val['false_consumption_amount'];
				$val['put_price']=$val['divide'];
			    $val['add_time']=date("Y-m-d",$val['add_time']);


			}elseif($val['cooperation_type']=='cpa'){
				$val['data']=$val['false_num'];
				$val['put_price']=$val['unit_price'];
			    $val['add_time']=date("Y-m-d",$val['add_time']);

			}
			
			return $val;
		}, $data);
    	// if ($data) {
			$result['code']=200;
			$result['msg']='成功';
			$result['data']['total_page']=$count;
			$result['data']['list']=$data;
		// }else{
		// 	$result['code']=501;
		// 	$result['msg']='查询成功';
		// }
		$this->ajaxReturn($result);

    }


}